{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 15.7 \u4eceC\u6269\u5c55\u4e2d\u91ca\u653e\u5168\u5c40\u9501\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u8ba9C\u6269\u5c55\u4ee3\u7801\u548cPython\u89e3\u91ca\u5668\u4e2d\u7684\u5176\u4ed6\u8fdb\u7a0b\u4e00\u8d77\u6b63\u786e\u7684\u6267\u884c\uff0c\n\u90a3\u4e48\u4f60\u5c31\u9700\u8981\u53bb\u91ca\u653e\u5e76\u91cd\u65b0\u83b7\u53d6\u5168\u5c40\u89e3\u91ca\u5668\u9501\uff08GIL\uff09\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728C\u6269\u5c55\u4ee3\u7801\u4e2d\uff0cGIL\u53ef\u4ee5\u901a\u8fc7\u5728\u4ee3\u7801\u4e2d\u63d2\u5165\u4e0b\u9762\u8fd9\u6837\u7684\u5b8f\u6765\u91ca\u653e\u548c\u91cd\u65b0\u83b7\u53d6\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "#include \"Python.h\"\n...\n\nPyObject *pyfunc(PyObject *self, PyObject *args) {\n   ...\n   Py_BEGIN_ALLOW_THREADS\n   // Threaded C code.  Must not use Python API functions\n   ...\n   Py_END_ALLOW_THREADS\n   ...\n   return result;\n}"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u53ea\u6709\u5f53\u4f60\u786e\u4fdd\u6ca1\u6709Python C API\u51fd\u6570\u5728C\u4e2d\u6267\u884c\u7684\u65f6\u5019\u4f60\u624d\u80fd\u5b89\u5168\u7684\u91ca\u653eGIL\u3002\nGIL\u9700\u8981\u88ab\u91ca\u653e\u7684\u5e38\u89c1\u7684\u573a\u666f\u662f\u5728\u8ba1\u7b97\u5bc6\u96c6\u578b\u4ee3\u7801\u4e2d\u9700\u8981\u5728C\u6570\u7ec4\u4e0a\u6267\u884c\u8ba1\u7b97\uff08\u6bd4\u5982\u5728numpy\u4e2d\uff09\n\u6216\u8005\u662f\u8981\u6267\u884c\u963b\u585e\u7684I/O\u64cd\u4f5c\u65f6\uff08\u6bd4\u5982\u5728\u4e00\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26\u4e0a\u8bfb\u53d6\u6216\u5199\u5165\u65f6\uff09\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5f53GIL\u88ab\u91ca\u653e\u540e\uff0c\u5176\u4ed6Python\u7ebf\u7a0b\u624d\u88ab\u5141\u8bb8\u5728\u89e3\u91ca\u5668\u4e2d\u6267\u884c\u3002\nPy_END_ALLOW_THREADS \u5b8f\u4f1a\u963b\u585e\u6267\u884c\u76f4\u5230\u8c03\u7528\u7ebf\u7a0b\u91cd\u65b0\u83b7\u53d6\u4e86GIL\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}